<?php
	/**
	* @File name:	new.php
	* @Desription:	description
	* @Author:	 	jinxin<alucard.jx@gmail.com>   
	* @Date:	 	Fri Sep 16 17:06:39 CST 2011
	* @Copyright 	Copytright (c) jinxin<alucard.jx@gmail.com>
	* @version	 	$Id $
	*/
	
	class admin_poll_controller_new extends controller {
		public $o;
		public function __construct($app,$controller,$site) {
			parent::__construct($app,$controller,$site);
			$this->o =  model::factory('admin_poll_model_admin','poll');
			$this->staticPath = POLL_PATH.'html/';
		}
		
		public function index(){}
		
		public function create(){
			$body['title'] = htmlspecialchars(trim($_POST['vtitle']));
			$body['start'] = strtotime(trim($_POST['start']));
			$body['end'] = strtotime(trim($_POST['end']));
			$body['url'] = htmlspecialchars(trim($_POST['url']));
			$body['type'] = (int)$_POST['vcateid'] > 0 && isset($_SERVER['config']['poll']['type'][(int)$_POST['vcateid']]) ? (int)$_POST['vcateid'] : 0;
			$body['userlimit'] = isset($_POST['vuserlimit']) ? 1 : 0;
			//$body['orderby'] = isset($_POST['orderby']) ? 1 : 0;
			$body['isroll'] = isset($_POST['isroll']) && $body['type'] > 4 ? 1 : 0;
			$body['choosetype'] = $body['type'] < 4 ? ($body['type'] == 1 && in_array(trim($_POST['type']),array('radio','checkbox')) ? trim($_POST['type']) : 'radio') : 'main';
			$body['maxlimit'] = $body['type'] == 1 && $body['choosetype'] == 'checkbox' && (int)$_POST['maxlimit'] > 0  ? (int)$_POST['maxlimit'] : 0;
			$body['minlimit'] = $body['type'] == 1 && $body['choosetype'] == 'checkbox' && (int)$_POST['minlimit'] >= 1 && $body['maxlimit'] > 0  && (int)$_POST['minlimit'] < $body['maxlimit'] ? (int)$_POST['minlimit'] : 1;
			$body['author_id'] = self::$login['uid'];
			$body['author'] = self::$login['username'];
			$body['create_time'] = $_SERVER['REQUEST_TIME'];
			$pid = $this->o->addpoll($body);
			switch ($body['type']){
				case '1':
				case '2':
				case '3':
					$i = 0;
					foreach ($_POST['keys'] as $key=>$v){
						$title = htmlspecialchars(trim($v));
						if ($title != '') {
							$keys[$i]['tid'] = 0;
							$keys[$i]['pid'] = $pid;
							$keys[$i]['title'] = $title;
							$keys[$i]['choosetype'] = $body['choosetype'];
							$keys[$i]['start_num'] = (int)$_POST['start_num'][$key] > 0 ? (int)$_POST['start_num'][$key]  : 0;
							$keys[$i]['num'] =$keys[$i]['start_num'];
							$i++;
						}
					}
					if (count($keys) > 0) {
						$this->o->addKeys($keys);
					}
					break;
				case '4':
				case '5':
					foreach ($_POST['question'] as $k=>$v){
						$title = htmlspecialchars(trim($v));
						if ($title != '') {
							$q['tid'] = 0;
							$q['pid'] = $pid;
							$q['title'] = $title;
							$q['choosetype'] = in_array($_POST['type'][$k],array('radio','checkbox')) ? $_POST['type'][$k] : 'radio';
							$q['maxlimit'] = (int)$_POST['maxlimit'][$k] > 0  ? (int)$_POST['maxlimit'][$k] : 0;
							$q['minlimit'] = (int)$_POST['minlimit'][$k] >= 1 && $q['maxlimit'] > 0  && (int)$_POST['minlimit'][$k] < $q['maxlimit'] ? (int)$_POST['minlimit'][$k] : 1;
							$qid = $this->o->addQes($q);
							$i=0;
							foreach ($_POST['keys'][$k] as $key=>$val){
								$title = htmlspecialchars(trim($val));
								if ($title != '') {
									$keys[$i]['tid'] = $qid;
									$keys[$i]['pid'] = $pid;
									$keys[$i]['title'] = $title;
									$keys[$i]['choosetype'] = $q['choosetype'];
									$keys[$i]['start_num'] = (int)$_POST['start_num'][$k][$key] > 0 ? (int)$_POST['start_num'][$key]  : 0;
									$keys[$i]['num'] =$keys[$i]['start_num'];
									$i++;
								}
							}
							if (count($keys) > 0) {
								$this->o->addKeys($keys);
							}
						}
					}
					
					
					$i= 0;
					if ($_POST['fields']) {
						foreach ($_POST['fields'] as $key=>$value){
							if (trim($value) != '') {
								$field[$i]['pid'] = $pid;
								$field[$i]['fieldname'] = htmlspecialchars(trim($value));
								$field[$i]['type'] = in_array($_POST['fieldtype'][$key],array('text','file')) ? $_POST['fieldtype'][$key] : 'text';
								$i++;
							}
						}
						$this->o->addFields($field);
					}
					
					break;
				case '6':
					
			}
			$this->createTpl($pid);
			$this->hint('创建投票成功','access','/poll/list/viewPoll/id/'.$pid);
		}
		
		private function createTpl($pid){
			$data = $this->o->getPoll($pid);
			$this->rander = 'static';
		}
	}
	